buildscript {
    ext.cubaVersion = '7.2.7'
    repositories {
        maven {
            url 'https://dl.bintray.com/cuba-platform/main'

        }
        jcenter()

        maven {
            url 'https://cuba-platform.bintray.com/premium'
            credentials {
                username(rootProject.hasProperty('bintrayPremiumRepoUser') ?
                        rootProject['bintrayPremiumRepoUser'] : System.getenv('CUBA_PREMIUM_USER'))
                password(rootProject.hasProperty('premiumRepoPass') ?
                        rootProject['premiumRepoPass'] : System.getenv('CUBA_PREMIUM_PASSWORD'))
            }
        }
        mavenLocal()
    }
    dependencies {
        classpath "com.haulmont.gradle:cuba-plugin:$cubaVersion"
    }
}

def modulePrefix = 'taxi'

def globalModule = project(":${modulePrefix}-global")
def coreModule = project(":${modulePrefix}-core")
def webModule = project(":${modulePrefix}-web")
def webToolkitModule = project(":${modulePrefix}-web-toolkit")

def servletApi = 'javax.servlet:javax.servlet-api:3.1.0'

apply(plugin: 'cuba')

cuba {
    artifact {
        group = 'ru.otus.taxicontrol'
        version = '0.1'
        isSnapshot = true
    }
    tomcat {
        }
}

dependencies {
    appComponent("com.haulmont.cuba:cuba-global:$cubaVersion")
    appComponent("com.haulmont.addon.maps:maps-global:1.3.0")
    appComponent('com.haulmont.addon.helium:helium-global:0.2.0')
    appComponent("com.haulmont.charts:charts-global:$cubaVersion")
}

def hsql = 'org.hsqldb:hsqldb:2.4.1'

configure([globalModule, coreModule, webModule, webToolkitModule]) {
    apply(plugin: 'java')
    apply(plugin: 'maven')
    apply(plugin: 'cuba')

    dependencies {
        testCompile('org.junit.jupiter:junit-jupiter-api:5.5.2')
        testCompile('org.junit.jupiter:junit-jupiter-engine:5.5.2')
        testCompile('org.junit.vintage:junit-vintage-engine:5.5.2')
    }

    task sourceJar(type: Jar) {
        from file('src')
        classifier = 'sources'
    }

    artifacts {
        archives sourceJar
    }
    test {
        useJUnitPlatform()
    }
}


configure(globalModule) {
    dependencies {
        if (!JavaVersion.current().isJava8()) {
            compile('ru.otus:common:1.0.0')
            runtime('javax.xml.bind:jaxb-api:2.3.1')
            runtime('org.glassfish.jaxb:jaxb-runtime:2.3.1')
        }
    }

    entitiesEnhancing {
        main {
            enabled = true
        }
    }
}

configure(coreModule) {

    configurations {
        jdbc
        dbscripts
    }

    dependencies {
        compile(globalModule)
        compileOnly(servletApi)
        jdbc(hsql)
        testRuntime(hsql)
    }

    task cleanConf(description: 'Cleans up conf directory', type: Delete) {
        delete "$cuba.appHome/${modulePrefix}-core/conf"
    }

    task deploy(dependsOn: [assemble, cleanConf], type: CubaDeployment) {
        appName = "${modulePrefix}-core"
        appJars(modulePrefix + '-global', modulePrefix + '-core')
    }

    task createDb(dependsOn: assembleDbScripts, description: 'Creates local database', type: CubaDbCreation) {
    }

    task updateDb(dependsOn: assembleDbScripts, description: 'Updates local database', type: CubaDbUpdate) {
    }
}

configure(webModule) {
    configurations {
        webcontent
    }

    dependencies {
        compileOnly(servletApi)
        compile(globalModule)
        compile('org.springframework:spring-websocket:5.2.8.RELEASE')
        compile('org.springframework:spring-messaging:5.2.8.RELEASE')
        compile('ru.otus:common:1.0.0')
    }

    jar {
        with copySpec {
            from sourceSets.main.allJava
            include "ru/otus/taxicontrol/web/toolkit/ui/client/**"
        }
    }

    task webArchive(type: Zip) {
        from file("$buildDir/web")
        from file('web')
        classifier = 'web'
    }

    artifacts {
        archives webArchive
    }

    task deployConf(type: Copy) {
        from file('src')
        include "ru/otus/taxicontrol/**"
        into "$cuba.appHome/${modulePrefix}/conf"
    }

    task clearMessagesCache(type: CubaClearMessagesCache) {
        appName = "${modulePrefix}"
    }
    deployConf.dependsOn clearMessagesCache

    task cleanConf(description: 'Cleans up conf directory', type: Delete) {
        delete "$cuba.appHome/${modulePrefix}/conf"
    }

    task deploy(dependsOn: [assemble, cleanConf], type: CubaDeployment) {
        appName = "${modulePrefix}"
        appJars(modulePrefix + '-global', modulePrefix + '-web')
    }
}

configure(webToolkitModule) {
    dependencies {
        compile(webModule)
    }

    jar {
        from sourceSets.main.allJava
    }

    task buildWidgetSet(type: CubaWidgetSetBuilding) {
        widgetSetClass = 'ru.otus.taxicontrol.web.toolkit.ui.AppWidgetSet'
    }

    task debugWidgetSet(type: CubaWidgetSetDebug) {
        widgetSetClass = 'ru.otus.taxicontrol.web.toolkit.ui.AppWidgetSet'
    }

    task webArchive(dependsOn: buildWidgetSet, type: Jar) {
        from file("$buildDir/web")
        classifier = 'client'
    }

    artifacts {
        archives webArchive
    }

    task deploy(dependsOn: webArchive, type: Copy) {
        from webArchive
        into "$cuba.tomcat.dir/webapps/${modulePrefix}/WEB-INF/lib"
    }
}


task undeploy(type: Delete, dependsOn: ":${modulePrefix}-web:cleanConf") {
    delete("$cuba.tomcat.dir/shared")
    delete("$cuba.tomcat.dir/webapps/${modulePrefix}-core")
    delete("$cuba.tomcat.dir/webapps/${modulePrefix}")
}

task restart(dependsOn: ['stop', ":${modulePrefix}-core:deploy", ":${modulePrefix}-web:deploy", ":${modulePrefix}-web-toolkit:deploy"], description: 'Redeploys applications and restarts local Tomcat') {
    doLast {
        ant.waitfor(maxwait: 6, maxwaitunit: 'second', checkevery: 2, checkeveryunit: 'second') {
            not {
                socket(server: 'localhost', port: '8787')
            }
        }
    }
}
task buildWar(type: CubaWarBuilding) {
    appProperties = ['cuba.automaticDatabaseUpdate': 'true']
    singleWar = false
    includeJdbcDriver = true
}
restart.finalizedBy start

